const Fs = require('fs-extra')
let path = ''
const arr = []
const obj = {}

/**
 * 向主进程发送数据
 */
function postMsg() {
  process.send(...arguments)
}

const handleData = ({ path: filePath, rows, id }) => {
  if (filePath) {
    path = filePath
  }
  if (rows) {
    if (/Array/i.test(Object.prototype.toString.call(rows))) {
      arr.push(...rows)
      Fs.outputJSONSync(path, arr, { spaces: '\t' })
      postMsg({ complete: true, id })
    } else {
      Object.assign(obj, rows)
      Fs.outputJSONSync(path, obj, { spaces: '\t' })
      postMsg({ complete: true, id })
    }
  }
}
process.on('message', handleData)
